1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
| # https:
# 1.在小程序中使用 # import bus from 'iny-bus'
onLoad () { this.eventId = bus.on('事件名', (a, b, c, d) => { console.log(a, b, c, d)
this.setData({ a, b, c } }) }
# onUnload () { bus.remove('事件名', this.eventId) }
onClick () { bus.emit('事件名', a, b, c) } # # # # # #
import { Event } from '../types' import { createUid, once } from '../utils'
class EventBus { * 储存事件的容器 */ private events: Event[] = []
* on 新增事件监听 * @param name 事件名 * @param execute 回调函数 * @param ctx 上下文 this * @returns { string } eventId 事件ID,用户取消该事件监听 */
on(name: string, execute: Function, ctx?: any): string { return this.addEvent(name, execute, ctx) }
* one 只允许添加一次事件监听 * @param name 事件名 * @param execute 回调函数 * @param ctx 上下文 this * @returns { string } eventId 事件ID,用户取消该事件监听 */
once(name: string, execute: Function, ctx?: any): string { return this.addEvent(name, once(execute), ctx) }
* remove 移除事件监听 * @param name 事件名 * @param eventId 移除单个事件监听需传入 * @returns { EventBus } EventBus EventBus 实例 */
remove(name: string, eventId: string): EventBus { const events = this.events
const index = events.findIndex(event => event.name === name)
if (index === -1) { return this }
if (!eventId) { events.splice(index, 1)
return this }
const executeIndex = events[index].executes.findIndex(item => item.id === eventId)
if (executeIndex !== -1) { events[index].executes.splice(executeIndex, 1) }
return this }
* emit 派发事件 * @param name 事件名 * @param args 其余参数 * @returns { EventBus } EventBus EventBus 实例 */
emit(name: string, ...args: any[]): EventBus { const event = this.find(name)
if (!event) { return this } const funcs = event.executes
funcs.forEach(func => { if (func.ctx) { return func.execute.apply(func.ctx, args) } func.execute(...args) })
return this }
* 查找事件的方法 * @param name */
find(name: string): Event | null { const events = this.events
for (let i = 0; i < events.length; i++) { if (name === events[i].name) { return events[i] } }
return null }
clear(): EventBus { this.events.length = 0
return this }
* 添加事件的方法 * @param name * @param execute */
private addEvent(name: string, execute: Function, ctx?: any): string { const eventId = createUid()
const events = this.events
const event = this.find(name)
if (event !== null) { event.executes.push({ id: eventId, execute, ctx })
return eventId }
events.push({ name, executes: [ { id: eventId, execute, ctx } ] })
return eventId } }
export default EventBus
|